#include "qtab_ui.h"
#include <QCheckBox>
#include <QComboBox>
#include <QDate>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPixmap>
#include <QScrollArea>
#include <QToolButton>
#include <QVBoxLayout>
#include "Manga_ui_logic.h"
#include "ZhouBian_ui.h"
#include "ZhouBian_ui_logic.h"
EventGridWidget *gridWidget = nullptr;
productgridwidget *gridWidget2 = nullptr;
QTabUi::QTabUi(ShoppingCartUI *newBuyCart, QWidget *parent)
    : QWidget(parent), m_newBuyCart(newBuyCart)
{
    setupUi();
}

void QTabUi::setupUi()
{
    m_tabWidget.setParent(this);
    m_tabWidget.move(10, 10);
    m_tabWidget.resize(1300, 900);
    m_tabWidget.setMinimumSize(1300, 900);
    m_tabWidget.setTabPosition(QTabWidget::North);
    m_tabWidget.setTabShape(QTabWidget::Rounded);
    m_tabWidget.setStyleSheet("QTabBar::tab { "
                              "position: absolute; "
                              "left: 50%; "
                              "transform: translateX(-50%);}");
    m_tabWidget.setTabsClosable(false);


    QWidget *w2 = new QWidget(&m_tabWidget);
    vb = new QVBoxLayout();

    lineEdit = new QLineEdit(w2);
    searchButton = new QToolButton(w2);
    lineEdit->setPlaceholderText(tr("点击搜索"));
    lineEdit->setStyleSheet("QLineEdit { border: 2px solid blue; }");
    searchButton->setIcon(QIcon(":/icon/light_search.png"));
    searchButton->setToolTip(tr("点击搜索"));
    searchButton->setCursor(Qt::ArrowCursor);

    // Search bar layout
    QHBoxLayout *layout2 = new QHBoxLayout();
    layout2->addStretch(1);
    layout2->addWidget(lineEdit);
    layout2->addWidget(searchButton);
    layout2->setContentsMargins(0, 0, 0, 0);
    layout2->addStretch(1);
    vb->addLayout(layout2);

    lb1 = new QLabel("城市：", w2);
    lb2 = new QLabel("筛选：", w2);

    // City selection dropdown
    cityComboBox = new QComboBox(w2);  // Use the member variable here
    cityComboBox->addItems({"全国", "北京", "上海", "广州", "深圳"});

    // Filter selection dropdown
    filterComboBox = new QComboBox(w2);  // Use the member variable here
    filterComboBox->addItems({"无排序", "日期", "价格", "销量"});

    // Combined layout for lb1, lb2, cityComboBox, and filterComboBox
    QHBoxLayout *combinedLayout = new QHBoxLayout();
    combinedLayout->setSpacing(5);  // Set minimal spacing between widgets
    combinedLayout->setContentsMargins(0, 0, 0, 0);  // Remove content margins
    lb1->setFixedWidth(40);  // Adjust as needed
    lb2->setFixedWidth(40);  // Adjust as needed
    lb1->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    lb2->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    combinedLayout->addWidget(lb1);
    combinedLayout->addWidget(cityComboBox);
    combinedLayout->addWidget(lb2);
    combinedLayout->addWidget(filterComboBox);

    // Add combined layout to the main vertical layout
    vb->addLayout(combinedLayout);

    //shoppingCartLogic = new ShoppingCartLogic(m_newBuyCart, this);
    gridWidget = new EventGridWidget( &m_tabWidget);

    QPixmap image(":/icon/logo.png");
    QDate date = QDate::currentDate();
    QString theme = "XXXX漫画演出";
    QString location = "Shanghai";
    QString price = "￥100";
    QPixmap Buyimage(":/icon/light_cart.png");

    QHBoxLayout *layout5 = new QHBoxLayout();
    layout5->addWidget(gridWidget);
    vb->addLayout(layout5);

    w2->setLayout(vb);
    scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->setWidget(w2);
    m_tabWidget.addTab(scrollArea, "漫画演出");
    QWidget *widget = new QWidget(&m_tabWidget);
    QVBoxLayout *vb2 = new QVBoxLayout();

    lineEdit2 = new QLineEdit(widget);
    searchButton2 = new QToolButton(widget);

    lineEdit2->setPlaceholderText(tr("点击搜索"));
    lineEdit2->setStyleSheet("QLineEdit { border: 2px solid blue; }");
    searchButton2->setIcon(QIcon(":/icon/light_search.png"));
    searchButton2->setToolTip(tr("点击搜索"));
    searchButton2->setCursor(Qt::ArrowCursor);

    QHBoxLayout *layout7 = new QHBoxLayout();
    layout7->addStretch(1); // 添加拉伸因子以居中对齐
    layout7->addWidget(lineEdit2);
    layout7->addWidget(searchButton2);
    layout7->setContentsMargins(0, 0, 0, 0);
    layout7->addStretch(1);

    vb2->addLayout(layout7);
    widget->setLayout(vb2);

    // Create labels and combo boxes
    lb11 = new QLabel("分类：", widget);
    lb22 = new QLabel("排序：", widget);

    // Set fixed width for labels to reduce extra space
    lb11->setFixedWidth(40);
    lb22->setFixedWidth(40);

    // Align labels to the right for closer positioning to combo boxes
    lb11->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    lb22->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    // Define QStringLists for categories and sorting options
    QStringList productCategories = {"全部分类", "谷子", "日用品", "3c数码", "毛绒玩具", "手办", "盲盒"};
    QStringList sortingOptions = {"无排序", "新品", "价格", "销量"};

    // Create QComboBox for product categories and sorting options
    categoryComboBox = new QComboBox(widget);
    categoryComboBox->addItems(productCategories);
    sortComboBox = new QComboBox(widget);
    sortComboBox->addItems(sortingOptions);

    // Create a single horizontal layout for all elements
    QHBoxLayout *combinedLayout2 = new QHBoxLayout();
    combinedLayout2->setSpacing(5);  // Minimal spacing between widgets
    combinedLayout2->setContentsMargins(0, 0, 0, 0);  // Remove margins

    // Add widgets to the combined layout
    combinedLayout2->addWidget(lb11);
    combinedLayout2->addWidget(categoryComboBox);
    combinedLayout2->addWidget(lb22);
    combinedLayout2->addWidget(sortComboBox);

    // Add the combined layout to the main vertical layout
    vb2->addLayout(combinedLayout2);

    // Set the main layout for the widget
    widget->setLayout(vb2);

    // Set the main layout for the widget
    widget->setLayout(vb2);
    gridWidget2 = new productgridwidget( &m_tabWidget);
    QPixmap image2(":/icon/logo.png");
    QString title2 = "XXXX商品";
    QString price2 = "￥100";
    QPixmap Buyimage2(":/icon/light_cart.png");

    QHBoxLayout *layout99 = new QHBoxLayout();
    layout99->addWidget(gridWidget2);
    vb2->addLayout(layout99);
    widget->setLayout(vb2);

    scrollArea2 = new QScrollArea();
    scrollArea2->setWidgetResizable(true); // 设置滚动区域可调整大小
    scrollArea2->setWidget(widget);

    m_tabWidget.addTab(scrollArea2, "Pili周边");

    m_tabWidget.setCurrentIndex(0);
}

QTabWidget *QTabUi::getTabWidget()
{
    return &m_tabWidget;
}
QTabUi::~QTabUi() {}
